
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>商户管理</cite></a>
    <a><cite>商户审核</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">待审商户</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="type" id="type" lay-search="">
                    <option value="-99">商户类型</option>
                    <option value="1">平台账户</option>
                    <option value="2">私有账户</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">搜索</button>
              </div>
            </div>
          </div>
          <div class="layui-row">
            <div class="layuiAdmin-btns" style="margin-bottom: 10px;">

            </div>
          </div>
          <table id="Pay_Mgr_Audit_dataAll" lay-filter="Pay_Mgr_Audit_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="payBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="audit">审核</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script>
  layui.use(['admin', 'table', 'util','form'], function(){
    var $ = layui.$
      ,admin = layui.admin
      ,table = layui.table
      ,element = layui.element
        ,view =layui.view,
        layer=layui.layer
      ,form = layui.form;

    element.render('breadcrumb', 'breadcrumb');

    var tplStatus = function(d){
      if(d.state == -1) {
        return "<span style='color: orange'>等待审核</span>";
      }else if(d.state == -2) {
        return "<span style='color: red'>审核不通过</span>";
      }
    };

    var tplType = function(d){
      if(d.kind == 1) {
        return "<span style='color: indigo'>平台账户</span>";
      }else if(d.kind == 2) {
        return "<span style='color: coral'>私有账户</span>";
      }

    };

    //商户列表
    table.render({
      elem: '#Pay_Mgr_Audit_dataAll'
      ,url: layui.setter.baseUrl + '/mch_audit/list' //列表接口
      ,where: {
          access_token: layui.data(layui.setter.tableName).access_token
      }
      ,id: 'tableReload'
      ,page: true
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'mchId', title: '商户ID'}
        ,{field: 'name', title: '商户名称'}
        ,{field: 'type', title: '商户类型', templet: tplType}
        ,{field: 'state', title: '状态', templet: tplStatus}
        ,{field: 'createTime', title: '创建时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
        ,{field: 'edit', title: '操作', width: 320, toolbar: '#payBar' }
      ]]
      ,skin: 'line'
    });

    //监听工具条
    table.on('tool(Pay_Mgr_Audit_dataAll)', function(obj){
      var data = obj.data;
      if(obj.event === 'audit'){
          var mchId =data.mchId;

          admin.req({
              type: 'get',
              url: layui.setter.baseUrl + '/mch_info/get',
              data: {
                  mchId : mchId
              },
              error: function(err){
                  layer.alert(JSON.stringify(err.field), {
                      title: '错误提示'
                  })
              },
              success: function(res){
                  if(res.code == 0){


                      admin.popup({
                          title: '审核商户'
                          , area: ['470px', '610px']
                          , id: 'LAY-popup-content-audit'
                          , success: function (layero, index) {
                              view(this.id).render('merchant/audit/mch_audit').done(function () {
                                  form.render(null, 'layuiadmin-app-form-list');

                                  var body =layer.getChildFrame('body',index);
                                  body.find('#email').val(data.email);
                                  form.val("layuiadmin-app-form-list",{

                                      "mchId":data.mchId

                                  })
                                  $('#name').val(res.data.name);
                                  $('#email').val(res.data.email);
                                  $('#mobile').val(res.data.mobile);
                                  if(res.data.kind == 1) {
                                      $("input[name='kind'][value='1']").attr("checked",true);
                                  }else if(res.data.kind == 2) {
                                      $("input[name='kind'][value='2']").attr("checked",true);
                                  }

                                  form.render();

                                  form.on('submit(audit)', function(data) {
                                      //这里可以写ajax方法提交表单
                                      admin.req({
                                          type: "POST",
                                          url: layui.setter.baseUrl + '/mch_audit/audit',
                                          data: data.field,
                                          success: function(res){
                                              if(res.code == 0) {
                                                  /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                      table.reload('tableReload');
                                                      layer.closeAll();
                                                  })*/
                                                  layer.msg('审核成功', {
                                                      offset: '15px'
                                                      ,icon: 1
                                                      ,time: 1000
                                                  }, function(){
                                                      table.reload("tableReload");
                                                      layer.closeAll();
                                                  });
                                              }else {
                                                  layer.msg('审核失败', {
                                                      offset: '15px'
                                                      ,icon: 1
                                                      ,time: 1000
                                                  });
                                              }
                                          }
                                      });
                                      return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                  });

                              });
                          }
                      });

                      form.render();
                  }
              }
          })



      } else if(obj.event === 'edit'){
          var mchId =data.mchId;

          admin.req({
              type: 'get',
              url: layui.setter.baseUrl + '/mch_info/get',
              data: {
                  mchId : mchId
              },
              error: function(err){
                  layer.alert(JSON.stringify(err.field), {
                      title: '错误提示'
                  })
              },
              success: function(res){
                  if(res.code == 0){

                      admin.popup({
                          title: '修改商户'
                          , area: ['470px', '610px']
                          , id: 'LAY-popup-content-edit'
                          , success: function (layero, index) {
                              view(this.id).render('merchant/audit/mch_edit').done(function () {
                                  form.render(null, 'layuiadmin-app-form-list');

                                  var body =layer.getChildFrame('body',index);
                                  body.find('#email').val(data.email);
                                  form.val("layuiadmin-app-form-list",{

                                      "mchId":data.mchId

                                  })
                                  $('#name').val(res.data.name);
                                  $('#email').val(res.data.email);
                                  $('#mobile').val(res.data.mobile);
                                  if(res.data.kind == 1) {
                                      $("input[name='kind'][value='1']").attr("checked",true);
                                  }else if(res.data.type == 2) {
                                      $("input[name='kind'][value='2']").attr("checked",true);
                                  }
                                  if(res.data.state == 1) {
                                      $("input[name='state'][value='1']").attr("checked",true);
                                  }else {
                                      $("input[name='state'][value='0']").attr("checked",true);
                                  }

                                  form.render();

                                  form.on('submit(userEdit)', function(data) {
                                      //这里可以写ajax方法提交表单
                                      admin.req({
                                          type: "POST",
                                          url: layui.setter.baseUrl + '/mch_info/update',
                                          data: data.field,
                                          success: function(res){
                                              if(res.code == 0) {
                                                  /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                      table.reload('tableReload');
                                                      layer.closeAll();
                                                  })*/
                                                  layer.msg('保存成功', {
                                                      offset: '15px'
                                                      ,icon: 1
                                                      ,time: 1000
                                                  }, function(){
                                                      table.reload("tableReload");
                                                      layer.closeAll();
                                                  });
                                              }else {
                                                  /*layer.alert(res.msg, {
                                                      title: '保存失败'
                                                  });*/
                                                  layer.msg('保存失败', {
                                                      offset: '15px'
                                                      ,icon: 1
                                                      ,time: 1000
                                                  });
                                              }
                                          }
                                      });
                                      return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                  });

                              });
                          }
                      });

                      form.render();
                  }
              }
          })


      }
    });

    // 搜索
    var $ = layui.$, active = {
      reload: function(){
        var mchId = $('#mchId').val();
        var type = $("#type").val();
        //执行重载
        table.reload('tableReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
            mchId: mchId,
            kind : kind
          }
        });
      }
    };
    $('#search').on('click', function() {
      var kind = $(this).data('kind');
      active[kind] ? active[kind].call(this) : '';
    });

    // 渲染表单
    form.render();
  });
</script>